/**
 * 
 */
package com.flashdeck.utils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author Vincentas
 *
 */
public class DecksTags {
	
	private Map<String, Set<String>> tagDecks = new HashMap<String, Set<String>>();
	
	private Map<String, Set<String>> deckTags = new HashMap<String, Set<String>>();
	
	public void addDeck(String deckName, Set<String> tags) {
		for (String tag : tags) {
			Set<String> decks = tagDecks.get(tag);
			if (decks == null) {
				decks = new HashSet<String>();
				tagDecks.put(tag, decks);
			}
			
			decks.add(deckName);
		}
		
		deckTags.put(deckName, tags);
	}
	
	public Set<String> getAvailableTaqs(Set<String> selectedTags) {		
		Set<String> result = new HashSet<String>();
		if (selectedTags.isEmpty()) {
			result = new HashSet<String>(tagDecks.keySet());
		}
		
		for (String tag : selectedTags) {
			Set<String> decks = tagDecks.get(tag);
			for (String deck : decks) {
				if (deckTags.get(deck).containsAll(selectedTags)) {
					result.addAll(deckTags.get(deck));
				}
			}
		}
		result.removeAll(selectedTags);
		
		return result;		
	}
	
	public Set<String> getDecks(Set<String> selectedTags) {
		Set<String> result = new HashSet<String>(deckTags.keySet());
		
		for (String tag : selectedTags) {			
			result.retainAll(tagDecks.get(tag));
		}
		
		return result;
	}

}
